(function($){

$.sharetime = $.sharetime || {};
$.ui = $.ui || {};

Function.prototype.bind = function(obj){
	var args = Array.prototype.slice.call(arguments, 1);
	var __method = this;
    return function() {
        return __method.apply(obj, args);
    }
};

Function.prototype.inherits = function(clazz){
	function tmp(){};
	tmp.prototype = clazz.prototype;
	this.prototype = new tmp();
	this.prototype.constructor = this;
};

if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
};

Array.prototype.remove = function(item){
	var i = 0;
	var len = this.length;
	while (i < len){
		if (this[i] === item){
			this.splice(i, 1);
			len--;
		} else {
			i++;
		}
	}
	return this;
};

Array.prototype.peek = function() {
    return this[this.length - 1]
};

if (!Array.prototype.shift) {
    Array.prototype.shift = function() {
        var a;
        if (this.length) {
            a = this[0];
            for (var b = 0; b < this.length - 1; b++) {
                this[b] = this[b + 1];
            }
            this.length--;
        }
        return a
    }
};
if (!Array.prototype.unshift) {
    Array.prototype.unshift = function(a) {
        var b = arguments.length;
        for (var c = this.length - 1; c >= 0; c--) {
            this[c + b] = this[c];
        }
        for (var d = 0; d < b; d++) {
            this[d] = arguments[d];
        }
        return this.length;
    }
};

$.sharetime.onReady = function(){
	var $bd = $('body');
	
	var cls =
        $.browser.msie ? "ie " + ($.browser.version == '6.0' ? 'ie6' : 'ie7')
            : $.browser.mozilla ? "mozilla"
            : $.browser.opera ? "opera"
            : $.browser.safari ? "safari" : "";
    
    $bd.addClass(cls);
};

var idSeed = 0;

$.utils = {
	isArray : function(v){
		return v && typeof v.pop == 'function';
	},
	
	isFunction: function(o) {
        return typeof o === 'function';
    },
    
    isObject: function(o) {
		return (o && (typeof o === 'object' || $.utils.isFunction(o))) || false;
    },
	
	formatString : function(format){
    	var args = Array.prototype.slice.call(arguments, 1);
    	return format.replace(/\{(\d+)\}/g, function(m, i){
         	return args[i];
    	});
    },
	
	//判断元素el的祖先里是否有符合selector的元素，如果有，返回这个元素，否则返回null
	findParent : function(el, selector, maxDepth){
		
		var b = document.body, depth = 0;
		maxDepth = maxDepth || 50;
		
		var p = el;
		
		while(p && p.nodeType == 1 && depth < maxDepth && p != b){
			if($(p).is(selector)){
				return p;
			}
			depth++;
			p = p.parentNode;
		}
		
		return null;
	},
	
	id : function(el, prefix){
       prefix = prefix || 'gen';
       var id = prefix + (++idSeed);
       return el ? (el.id ? el.id : (el.id = id)) : id;
    }
};

$.datemath = {
	
	msInDay : 1000 * 60 * 60 * 24,
    
    clearTime : function(dateObj) {
        dateObj.setHours(12, 0, 0, 0);
        return date;
    },

    getFirstDayOfWeek : function(dateObj) {
        return dateObj.add(Date.DAY, - dateObj.getDay());
    },

    getLastDayOfWeek : function(dateObj){
        return dateObj.add(Date.DAY, 6 - dateObj.getDay());
    },

    toKey : function(dateObj) {
        return Math.floor((dateObj.getTime() - (dateObj.getTimezoneOffset() * 60000)) / 86400000);
    },

    isWeekend : function(dateObj) {
        return dateObj.getDay() == 0 || dateObj.getDay() == 6;
    },

    isToday : function(dateObj) {
        var today = new Date();
        return this.toKey(today) == dateObj.toKey(dateObj);
    },

    zeroTime : function(dateObj) {
        dateObj.setHours(0);
        dateObj.setMinutes(0);
        dateObj.setSeconds(0);
        dateObj.setMilliseconds(0);
    },

    rangeToIndex : function(range) {
        var tzOffset = range.getTimezoneOffset();
        return Math.floor((range.getTime() - tzOffset * 60 * 1000) / this.msInDay);
    },

    indexToRange:function(index) {
        var dateObj = new Date();
        var tzOffset = dateObj.getTimezoneOffset();
        dateObj.setTime(index * this.msInDay + (tzOffset + 60) * 60 * 1000);
        dateObj.setHours(0);
        return dateObj;
    },

    toServerTime: function(d) {
        return this.toDateString(this.toUTC(d));
    },

    toLocalTime: function(t) {
        if (t == undefined) return '';
        if (t.length > 11) {
            return new Date(Date.UTC(
                    parseInt(t.substring(0, 4), 10),
                    parseInt(t.substring(5, 7), 10) - 1,
                    parseInt(t.substring(8, 10), 10),
                    parseInt(t.substring(11, 13), 10),
                    parseInt(t.substring(14, 16), 10),
                    parseInt(t.substring(17, 19), 10)));
        } else {
            return new Date(Date.UTC(
                    parseInt(t.substring(0, 4), 10),
                    parseInt(t.substring(5, 7), 10) - 1,
                    parseInt(t.substring(8, 10), 10),
                    0,
                    0,
                    0));
        }
    },
//将服务器端传来的时间转化为js对象
    toDateObject: function(d) {
        if (d.length > 11) {
            if (d)
                return new Date(
                        parseInt(d.substring(0, 4), 10),
                        parseInt(d.substring(5, 7), 10) - 1,
                        parseInt(d.substring(8, 10), 10),
                        parseInt(d.substring(11, 13), 10),
                        parseInt(d.substring(14, 16), 10),
                        parseInt(d.substring(17, 19), 10));
        } else {
            return new Date(
                    parseInt(d.substring(0, 4), 10),
                    parseInt(d.substring(5, 7), 10) - 1,
                    parseInt(d.substring(8, 10), 10),
                    0,
                    0,
                    0);
        }
        return '';
    },

    toUTC: function(d) {
        return new Date(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds());
    },

//将日期转化为string传上去，格式 2007-10-28T11:53:14Z
    toDateString: function(d) {
        var a = [];
        var dd = this.dd;
        a.push(d.getFullYear());
        a.push("-");
        a.push(dd(d.getMonth() + 1));
        a.push("-");
        a.push(dd(d.getDate()));
        a.push("T");
        a.push(dd(d.getHours()));
        a.push(":");
        a.push(dd(d.getMinutes()));
        a.push(":");
        a.push(dd(d.getSeconds()));
        a.push("Z");
        return a.join("");
    },

    dd: function(num) {
        if (num < 10) return "0" + num;
        return num;
    }
};

$.ui.indicator = {
	
	show : function(){
		//var wnd = $(window), doc = $(document);
		//var t = doc.scrollTop(), l = doc.scrollLeft();
		var $indicator = $('#loading');
        //t += (wnd.height() / 2) - ($indicator.height() / 2);
        //l += (wnd.width() / 2) - ($indicator.width() / 2);
        //t = t < doc.scrollTop() ? doc.scrollTop() : t;
        
        $("#overlay").show();
        //$("#indicator").css({'position' : 'absolute', 'top' : t, 'left' : l}).show();
        $indicator.show();
	},
	
	hide : function(){
		$("#overlay").hide();
		$("#loading").hide();
	}
};

$.sharetime.globalvars = {
	
}

})(jQuery);